package com.rulestech.salespro.unittest;

import java.io.IOException;

import org.apache.poi.ss.usermodel.Row;

import com.rulestech.framework.constants.Color;
import com.rulestech.framework.constants.Season;
import com.rulestech.framework.location.Location;
import com.rulestech.framework.unittest.DataDrivenTestCase;
import com.rulestech.framework.unittest.SpreadsheetTestCaseBuilder;
import com.rulestech.salespro.engine.SalesProEngineContext;
import com.rulestech.salespro.engine.SalesProEngineResult;
import com.rulestech.salespro.inventory.InventoryItem;
import com.rulestech.salespro.inventory.Product;
import com.rulestech.salespro.inventory.ScoreBasedDiscount;

public class SalesProTestCaseBuilder extends SpreadsheetTestCaseBuilder {

	public SalesProTestCaseBuilder(String fileName)
			throws IOException {
		super(fileName);
	}


	@Override
	protected DataDrivenTestCase convertOneRowPerTestCase(Row row) {
		SalesProEngineContext context = getContextTestData(row);
		SalesProEngineResult result = getResult(row);
		return new DataDrivenTestCase(getCellValueAsString(row, SalesProDataElements.TEST_CASE_ID.name()), context, result);
	}

	private SalesProEngineResult getResult(Row row){
		SalesProEngineResult result = new SalesProEngineResult();
		ScoreBasedDiscount discount = new ScoreBasedDiscount(0, getCellValueAsBoolean(row, SalesProDataElements.DISCOUNT_SETTING_DEFAULT.name()));
		discount.setWeight(ScoreBasedDiscount.Parameter.INVENTORY, getCellValueAsDouble(row, SalesProDataElements.DISCOUNT_WEIGHT_INVENTORY.name()));
		discount.setRate(getCellValueAsDouble(row, SalesProDataElements.DISCOUNT_RATE.name()));
		result.setDiscount(discount);
		return result;
	}
	
	private SalesProEngineContext getContextTestData(Row row){
		Location location = new Location();
		return new SalesProEngineContext(location, getInventoryItemTestData(row));
	}
 
	private InventoryItem getInventoryItemTestData(Row row){
		return new InventoryItem(0, getProductTestData(row), getCellValueAsInt(row, SalesProDataElements.INVENTORY_ITEM_QUANTITY.name()), 
				getCellValueAsInt(row, SalesProDataElements.INVENTORY_ITEM_SOLD_QUANTITY.name()), 
				getCellValueAsInt(row, SalesProDataElements.INVENTORY_ITEM_TURN_AROUND_DAYS.name()));
	}


	private Product getProductTestData(Row row) {
		return new Product(0, getCellValueAsString(row, SalesProDataElements.PRODUCT_NAME.name()), 
				getCellValueAsString(row, SalesProDataElements.PRODUCT_SKU.name()), 
				"", 
				getCellValueAsInt(row, SalesProDataElements.PRODUCT_SHELF_LIFE_IN_DAYS.name()), 
				getCellValueAsDouble(row, SalesProDataElements.PRODUCT_WHOLE_SALE_PRICE.name()), 
				getCellValueAsDouble(row, SalesProDataElements.PRODUCT_RETAIL_PRICE.name()), 
				getCellValueAsDouble(row, SalesProDataElements.PRODUCT_EXPENSE.name()), 
				getCellValueAsDouble(row, SalesProDataElements.PRODUCT_BREAKEVEN_PRICE.name()),
				getCellValueAsDouble(row, SalesProDataElements.PRODUCT_BREAKEVEN_MARGIN.name()), 
				"", Season.FALL, Color.RED, "", "");
	}
	 
}
 
